package com.ruoyi.web.service.stock.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.domain.stock.Retrieval;
import com.ruoyi.web.domain.stock.Stock;
import com.ruoyi.web.mapper.stock.RetrievalMapper;
import com.ruoyi.web.mapper.stock.StockMapper;
import com.ruoyi.web.pojo.dto.stock.RetrievalRep;
import com.ruoyi.web.service.stock.IRetrievalService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;


/**
 * 出库明显Service业务层处理
 * 
 * @author zq
 * @date 2024-01-08
 */
@Service
public class RetrievalService implements IRetrievalService
{

    private final RetrievalMapper retrievalMapper;
    private final StockMapper stockMapper;

    public RetrievalService(RetrievalMapper retrievalMapper, StockMapper stockMapper) {
        this.retrievalMapper = retrievalMapper;
        this.stockMapper = stockMapper;
    }

    /**
     * 查询出库明显
     * 
     * @param id 出库明显主键
     * @return 出库明显
     */
    @Override
    public Retrieval selectRetrievalById(Long id)
    {
        Retrieval retrieval = retrievalMapper.selectRetrievalById(id);
        if (SecurityUtils.isParkManager()){
            return retrieval;
        }else if (SecurityUtils.getUserId().equals(retrieval.getUserId())){
            return retrieval;
        }else {
            return null;
        }
    }

    /**
     * 查询出库明显列表
     * 
     * @param retrieval 出库明显
     * @return 出库明显
     */
    @Override
    public List<RetrievalRep> selectRetrievalList(Retrieval retrieval)
    {
        if (SecurityUtils.isParkManager()){
            retrieval.setUserId(null);
        }else {
            retrieval.setUserId(SecurityUtils.getUserId());
        }
        if (Objects.nonNull(retrieval.getStartTime()) && Objects.nonNull(retrieval.getEndTime())){
            retrieval.setStartTime(DateUtils.minDate(retrieval.getStartTime()));
            retrieval.setEndTime(DateUtils.maxDate(retrieval.getEndTime()));
        }
        return retrievalMapper.selectRetrievalList(retrieval);
    }

    /**
     * 新增出库明显
     * 
     * @param retrieval 出库明显
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertRetrieval(Retrieval retrieval)
    {
        retrieval.setCreateBy(SecurityUtils.getUsername());
        retrieval.setUpdateTime(DateUtils.getNowDate());
        retrieval.setUserId(SecurityUtils.getUserId());

        // 减库存
        Stock stock = getStock(retrieval);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String s = objectMapper.writeValueAsString(stock);
            retrieval.setRemark(s);
        } catch (Exception e) {
            throw new ServiceException("库存转换异常");
        }
        retrieval.setUnit(stock.getUnit());
        retrieval.setProduct(stock.getProduct());
        retrieval.setType(stock.getType());
        return retrievalMapper.insertRetrieval(retrieval);
    }

    private Stock getStock(Retrieval retrieval) {
        Stock stock = stockMapper.selectStockById(retrieval.getStockId());
        if (Objects.isNull(stock)){
            throw new ServiceException("未查询到库存");
        }
        if (stock.getNumber() < 1){
            throw new ServiceException("库存不足");
        }
        if (retrieval.getNumber() > stock.getNumber()){
            throw new ServiceException("库存不足");
        }
        stock.setNumber(stock.getNumber() - retrieval.getNumber());
        stock.setUpdateBy(SecurityUtils.getUsername());
        stock.setUpdateTime(DateUtils.getNowDate());
        stockMapper.updateStock(stock);
        return stock;
    }

    /**
     * 修改出库明显
     * 
     * @param retrieval 出库明显
     * @return 结果
     */
    @Override
    public int updateRetrieval(Retrieval retrieval)
    {
        retrieval.setUpdateTime(DateUtils.getNowDate());
        return retrievalMapper.updateRetrieval(retrieval);
    }

    /**
     * 批量删除出库明显
     * 
     * @param ids 需要删除的出库明显主键
     * @return 结果
     */
    @Override
    public int deleteRetrievalByIds(List<Long> ids)
    {
        return retrievalMapper.deleteRetrievalByIds(ids);
    }

    /**
     * 删除出库明显信息
     * 
     * @param id 出库明显主键
     * @return 结果
     */
    @Override
    public int deleteRetrievalById(Long id)
    {
        return retrievalMapper.deleteRetrievalById(id);
    }
}
